import { undoManager, buildNewCellDef, checkSelection } from "../util/Utils.js";
import $ from "jquery";

export default {
  // 设置边框
  setBorder (designer, border) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const table = context.hot;
    const selected = table.getSelected()[0];
    // const oldStyle = {};
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    let newBorder = {
      width: 1,
      color: "0,0,0",
      style: "solid"
    };
    // 无边框
    if (border === "none") {
      border = "";
      newBorder = "";
    }
    let oldBorderStyle = this.updateBorderStyles(context, startRow, startCol, endRow, endCol, newBorder, border);
    table.render();
    undoManager.add({
      redo: function () {
        oldBorderStyle = this.updateBorderStyles(context, startRow, startCol, endRow, endCol, newBorder, border);
        table.render();
        context.isModify = true;
      },
      undo: function () {
        this.updateOldBorderStyles(context, startRow, startCol, endRow, endCol, oldBorderStyle, border);
        context.isModify = true;
      }
    });
    context.isModify = true;
  },

  updateOldBorderStyles (context, startRow, startCol, endRow, endCol, oldBorderStyle) {
    const hot = context.hot;
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        // const td = hot.getCell(i, j);
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const oldBorder = oldBorderStyle[i + "," + j];
        const cellStyle = cellDef.cellStyle;
        cellStyle.leftBorder = oldBorder.leftBorder || "";
        cellStyle.rightBorder = oldBorder.rightBorder || "";
        cellStyle.topBorder = oldBorder.topBorder || "";
        cellStyle.bottomBorder = oldBorder.bottomBorder || "";
      }
    }
    hot.render();
  },

  updateBorderStyles (context, startRow, startCol, endRow, endCol, newBorder, target) {
    const oldStyle = {};
    // const hot = context.hot;
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        // const td = hot.getCell(i, j);
        const cellDef = context.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldStyle[i + "," + j] = {
          leftBorder: cellStyle.leftBorder,
          rightBorder: cellStyle.rightBorder,
          topBorder: cellStyle.topBorder,
          bottomBorder: cellStyle.bottomBorder
        };
        if (!target) {
          cellStyle.leftBorder = newBorder;
          cellStyle.rightBorder = newBorder;
          cellStyle.topBorder = newBorder;
          cellStyle.bottomBorder = newBorder;
        } else if (target === "left") {
          cellStyle.leftBorder = newBorder;
          cellStyle.rightBorder = "";
          cellStyle.topBorder = "";
          cellStyle.bottomBorder = "";
        } else if (target === "right") {
          cellStyle.rightBorder = newBorder;
          cellStyle.leftBorder = "";
          cellStyle.topBorder = "";
          cellStyle.bottomBorder = "";
        } else if (target === "top") {
          cellStyle.topBorder = newBorder;
          cellStyle.leftBorder = "";
          cellStyle.rightBorder = "";
          cellStyle.bottomBorder = "";
        } else if (target === "bottom") {
          cellStyle.bottomBorder = newBorder;
          cellStyle.leftBorder = "";
          cellStyle.rightBorder = "";
          cellStyle.topBorder = "";
        }
      }
    }
    return oldStyle;
  },
  // 合并单元格
  mergeCell (designer) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    const table = context.hot;
    let mergeCells = table.getSettings().mergeCells || [];
    let oldMergeCells = mergeCells.concat([]);
    const selected = table.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    this.doMergeCells(designer, startRow, startCol, endRow, endCol, table);
    undoManager.add({
      redo: function () {
        mergeCells = table.getSettings().mergeCells || [];
        oldMergeCells = mergeCells.concat([]);
        this.doMergeCells(designer, startRow, startCol, endRow, endCol, table);
        context.isModify = true;
      },
      undo: function () {
        table.updateSettings({ mergeCells: oldMergeCells });
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  doMergeCells (designer, startRow, startCol, endRow, endCol, table) {
    const { context } = designer;
    let doMerge = true;
    let doSplit = false;
    // const selectCell = context.getCell(startRow, startCol);
    const mergeCells = table.getSettings().mergeCells || [];
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const td = table.getCell(i, j);
        if (!td) {
          continue;
        }
        const $td = $(td);
        let colSpan = $td.prop("colspan") || "1";
        let rowSpan = $td.prop("rowspan") || "1";
        colSpan = parseInt(colSpan);
        rowSpan = parseInt(rowSpan);
        if (colSpan > 1 || rowSpan > 1) {
          let index = 0;
          doSplit = true;
          doMerge = false;
          while (index < mergeCells.length) {
            const mergeItem = mergeCells[index];
            const row = mergeItem.row;
            const col = mergeItem.col;
            if (row === i && col === j) {
              mergeCells.splice(index, 1);
              break;
            }
            index++;
          }
        }
      }
    }
    if (doMerge) {
      if (endRow < startRow) {
        const tmp = startRow;
        startRow = endRow;
        endRow = tmp;
      }
      if (endCol < startCol) {
        const tmp = startCol;
        startCol = endCol;
        endCol = tmp;
      }
      let rowSpan = endRow - startRow;
      let colSpan = endCol - startCol;
      if (rowSpan === 0) {
        rowSpan = 1;
      } else {
        rowSpan++;
      }
      if (colSpan === 0) {
        colSpan = 1;
      } else {
        colSpan++;
      }
      const newMergeItem = { row: startRow, col: startCol, rowspan: rowSpan, colspan: colSpan };
      mergeCells.push(newMergeItem);
    } else {
      if (doSplit) {
        for (let i = startRow; i <= endRow; i++) {
          for (let j = startCol; j <= endCol; j++) {
            let cellDef = context.getCell(i, j);
            if (!cellDef) {
              cellDef = buildNewCellDef(i + 1, j + 1);
              context.addCell(cellDef);
            }
          }
        }
      } else {
        this.$message.error(`请选择多个单元格后再进行此操作!`);
      }
    }
    table.updateSettings({ mergeCells });
  }
};
